/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.exam.dao;

import com.sh.ndt.common.DBObjects;
import com.sh.ndt.common.dao.ExtractDataRows;
import com.sh.ndt.exam.entity.ExamHistory;
import com.sh.ndt.exam.entity.TestHistory;
import com.sh.ndt.exam.mapper.ExamHistoryMapper;
import com.sh.ndt.exam.mapper.TestHistoryMapper;
import com.sh.ndt.exception.DAOException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 *
 * @author Prashanth
 */
public class ExamDAOImpl extends JdbcDaoSupport implements ExamDAO {

    private static final Logger LOGGER = Logger.getLogger(ExamDAOImpl.class);

    private ExamDAOImpl() {
        LOGGER.debug("****************** ExamDAOImpl Initialized **");
    }

    @Override
    public List<TestHistory> getTestHistoryList(long userId, long testId) throws DAOException {
        LOGGER.debug("Inside ExamDAOImpl's getTestHistoryList method...");

        try {
            SqlParameter sqlParam[] = new SqlParameter[3];

            sqlParam[0] = new SqlParameter("in_user_id", Types.INTEGER);
            sqlParam[1] = new SqlParameter("in_test_id", Types.INTEGER);
            sqlParam[2] = new SqlReturnResultSet("TEST_HISTORY_LIST", new TestHistoryMapper());

            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_GET_TEST_HISTORY, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_user_id", userId);
            inputs.put("in_test_id", testId);

            Map results = rows.executeStoredProcedure(inputs);
            List<TestHistory> testHistoryList = (List<TestHistory>) results.get("TEST_HISTORY_LIST");

            LOGGER.debug("Question Retrival was successful: " + testHistoryList.size());

            return testHistoryList;
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        return null;
    }

    @Override
    public List<ExamHistory> getExamHistoryList(long userId, long examId) throws DAOException {
        LOGGER.debug("Inside ExamDAOImpl's getTestHistoryList method...");

        try {
            SqlParameter sqlParam[] = new SqlParameter[3];

            sqlParam[0] = new SqlParameter("in_user_id", Types.INTEGER);
            sqlParam[1] = new SqlParameter("in_exam_id", Types.INTEGER);
            sqlParam[2] = new SqlReturnResultSet("TEST_EXAM_LIST", new ExamHistoryMapper());

            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_GET_EXAM_HISTORY, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_user_id", userId);
            inputs.put("in_exam_id", examId);
            
            Map results = rows.executeStoredProcedure(inputs);
            List<ExamHistory> examHistoryList = (List<ExamHistory>) results.get("TEST_EXAM_LIST");

            LOGGER.debug("Question Retrival was successful: " + examHistoryList.size());

            return examHistoryList;
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        return null;
    }

}
